#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include "myOpenCVLIB\myheader.h"

using namespace std;  
using namespace cv; 

#define W_HEIGHT 600
#define W_WIDTH 800
#define delay 20
#define WINDOW_NAME "RANDOM SHAPES"

int x_1 = -W_WIDTH/2;
int x_2 = W_WIDTH*3/2;
int y_1 = -W_WIDTH/2;
int y_2 = W_WIDTH*3/2;

const int maxEclipseSize = W_WIDTH/2;




const int NUMBER =  20;
void drawRandomCircle( Mat image, char* windowName, RNG rng );
void drawRandomEllipse(Mat image, char* windowName, RNG rng);
void drawRandomRectangle(Mat image, char* windowName, RNG rng);
void drawRandomLine(Mat image, char* windowName, RNG rng);
static Scalar randomColor( RNG& rng );

int main(){
	int c;
	
	RNG rng(getTickCount() );
	int i ;
	unsigned int count=0;

	Mat image = Mat::zeros(W_HEIGHT,W_WIDTH,CV_8UC3);
	Mat clone;
	
	imshow( WINDOW_NAME, image );
	waitKey( delay );

	do{
		rng(getTickCount());
		i = rng.next();
		if(i%2 ==0){
			drawRandomLine(image,WINDOW_NAME,rng);
		}
		if(i%43 ==0){
			drawRandomRectangle(image,WINDOW_NAME,rng);
		}
		if(i%31==0){
			drawRandomEllipse(image,WINDOW_NAME,rng);
		}
		if(i%3==0){
			drawRandomCircle(image,WINDOW_NAME,rng);
		}
		
		cout << count++ << '\xd';
		if( waitKey( delay ) >= 0){
			return -1;
		}
	}
	while(1);

	return 0;
}

static Scalar randomColor( RNG& rng )
{
  int icolor = (unsigned) rng;
  return Scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 );
}

void drawRandomLine(Mat image, char* windowName, RNG rng){
	
	Point p1,p2;

	//for(int i=0; i < NUMBER;i++){
		p1.x = rng.uniform(0,W_WIDTH);
		p1.y = rng.uniform(0,W_HEIGHT);
		p2.x = p1.x + rng.uniform(p1.x,W_WIDTH - p1.x) +1;
		p2.y = p1.y + rng.uniform(p1.y,W_HEIGHT - p1.y) +1;

		
		image.convertTo(image,-1,0.99);
		line(image, p1 ,p2 ,randomColor(rng) ,rng.uniform(1,10) ,8);
		imshow(windowName, image);	
	//}
}

void drawRandomRectangle(Mat image, char* windowName, RNG rng){
	Point p1,p2;

	//for(int i=0;i<NUMBER;i++){
		p1.x = rng.uniform(x_1,x_2);
		p1.y = rng.uniform(y_1,y_2);
		p2.x = rng.uniform(x_1,x_2);
		p2.y = rng.uniform(y_1,y_2);
		image.convertTo(image,-1,0.99);
		rectangle(image,p1,p2,randomColor(rng),rng.uniform(-3,10),8);
		imshow(windowName, image);	
	//}
}

void drawRandomEllipse(Mat image, char* windowName, RNG rng){
	Point p1;

	//for(int i=0;i<NUMBER;i++){
		p1.x = rng.uniform(0,W_WIDTH);
		p1.y = rng.uniform(0,W_HEIGHT);
		image.convertTo(image,-1,0.99);
		ellipse(image
			,p1
			,Size(rng.uniform(1,maxEclipseSize), rng.uniform(1,maxEclipseSize))
			,rng.uniform(0,360)
			,rng.uniform(0,360)
			,rng.uniform(0,360)
			,randomColor(rng)
			,rng.uniform(-1,10)
			,8);
		imshow(windowName, image);	
	//}
}

void drawRandomCircle( Mat image, char* windowName, RNG rng ){
 
    Point center;
    center.x = rng.uniform(x_1, x_2);
    center.y = rng.uniform(y_1, y_2);
	image.convertTo(image,-1,0.99);
    circle( image, center, rng.uniform(0, 300), randomColor(rng),
            rng.uniform(-1, 3), 8 );
	imshow(windowName, image);	

}

